package com.qilu.pe.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.dao.bean.Crowd;
import com.qilu.pe.dao.http.APIRetrofit;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class LifeHabitActivity extends Base2Activity {
    private RecyclerView rcv_life_habit, rcv_life_habit_all;// 已选择 全部
    private TextView tv_ok;

    private LifeHabitListAdapter adapter;
    private List<Crowd> crowdSelList = new ArrayList<>();// 已选择的习惯列表

    private List<Crowd> crowdAllList = new ArrayList<>();// 全部的习惯
    private LifeHabitListAdapter adapter2;

    private int adapt;// 适应人群

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_life_habit;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_life_habit = findViewById(R.id.rcv_life_habit);
        rcv_life_habit_all = findViewById(R.id.rcv_life_habit_all);
        tv_ok = findViewById(R.id.tv_ok);

        setHeadTitle("生活习惯");

        initHabitList();

        adapter = new LifeHabitListAdapter(this);
        adapter.type = 1;
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        rcv_life_habit.setLayoutManager(manager);
        rcv_life_habit.setAdapter(adapter);
        adapter.setDataList(crowdSelList);

        adapter2 = new LifeHabitListAdapter(this);
        adapter2.type = 2;
        GridLayoutManager manager2 = new GridLayoutManager(this, 3);
        rcv_life_habit_all.setLayoutManager(manager2);
        rcv_life_habit_all.setAdapter(adapter2);
        adapter2.setDataList(crowdAllList);

        setListeners();

        hideProgress();
        reqHabitList();
        showProgress();
    }

    private void setListeners() {
        setOnClickListener(tv_ok);
        adapter2.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Crowd crowd = crowdAllList.get(position);// 当前点击的对象
                for (int i = 0; i < crowdSelList.size(); i++) {
                    if (TextUtils.equals(crowdSelList.get(i).getName(), crowd.getName())) {
                        // 说明已经添加过该习惯
                        return;
                    }
                }
                crowdSelList.add(crowd);
                adapter.setDataList(crowdSelList);
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_ok) {// 查看检验
            if (crowdSelList.size() <= 1) {

            }
            Bundle bundle = new Bundle();
            bundle.putInt("adapt", adapt);
            bundle.putSerializable("crowdList", (Serializable) crowdSelList);
            startActivity(ItemListByHabitActivity.class, bundle);
        }
    }

    private void initHabitList() {
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            Crowd crowd = (Crowd) bundle.getSerializable("crowd");
            crowdSelList.add(crowd);

            adapt = bundle.getInt("adapt");
        }
    }

    // 请求习惯列表
    private Disposable reqHabitList() {
        return APIRetrofit.getDefault().reqHabitList("hehe", "hehe")
                .compose(RxUtil.<BaseResult2<List<Crowd>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<Crowd>>>() {
                    @Override
                    public void accept(BaseResult2<List<Crowd>> listResult2) throws Exception {
                        hideProgress();
                        if (listResult2.isSuccess()) {

                        } else {

                        }
                        crowdAllList = listResult2.getData();
                        adapter2.setDataList(crowdAllList);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private class LifeHabitListAdapter extends ListBaseAdapter<Crowd> {

        public int type = 1;// 1 选择的习惯 2 所有的习惯

        public LifeHabitListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_habit_sel_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, final int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            ImageView iv_del = holder.getView(R.id.iv_del);

            Crowd crowd = mDataList.get(position);
            tv_name.setText(crowd.getName());

            if (type == 1) {
                if (position == 0) {
                    iv_del.setVisibility(View.GONE);
                } else {
                    iv_del.setVisibility(View.VISIBLE);
                }
            } else {
                iv_del.setVisibility(View.GONE);
            }

            iv_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    crowdSelList.remove(position);
                    adapter.setDataList(crowdSelList);
                }
            });

        }
    }
}